/**
 * Created by Administrator on 2017/7/12.
 */
define(['jquery', "vue", 'ks-spa'], function ($, Vue, spa) {
    mui.init();
    //调用微信JS api 支付
    var jsApiParameters;

    function jsApiCall() {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            jsApiParameters,
            function (res) {
                if (res.err_msg === "get_brand_wcpay_request:ok") {
                    window.location.href = "{:url('Pay/pay_success')}";
                }
            }
        );
    }

    var vue = new Vue({
        el: '#pay_page',
        data: {},
        methods: {
            delivery_item_pay: function (id) {
                $.ajax({
                    type: 'POST',
                    data: {order_item_id: id},
                    url: delivery_item_pay_url,
                    dataType: 'json',
                    beforeSend: function (xhr, settings) {
                        spa.showLoading();
                    },
                    success: function (res, status, xhr) {
                        spa.hideLoading();
                        if (res.code === 1) {
                            mui.alert(res.msg, '系统提示', function () {
                                window.location.href = order_url;
                            }, 'div');
                        } else {
                            mui.alert(res.msg);
                        }
                    },
                    error: function (xhr, errorType, error) {
                        spa.hideLoading();
                        mui.toast('网络异常，请稍后重试');
                    },
                    complete: function (XMLHttpRequest, textStatus) {
                        spa.hideLoading();
                    }
                });

                // spa.showLoading();
                // var data = {
                //     order_item_id: id
                // };
                // $.post(delivery_item_pay_url, data, function (res) {
                //         spa.hideLoading();
                //         if (res.code === 1) {
                //             mui.alert("提交订单成功", '系统提示', function () {
                //                 window.location.href = order_url;
                //             }, 'div');
                //         } else {
                //             mui.alert(res.msg);
                //         }
                //     }
                // )
            },
            balance_item_pay: function (id) {
                mui.prompt("付款金额为" + money + "元", '支付密码', '请输入支付密码', ['取消', '确定'], function (e) {
                    if (e.index === 1) {
                        $.ajax({
                            type: "POST",
                            url: balance_item_pay_url,
                            data: {order_id: id, pay_password: e.value},// 要提交的表单
                            beforeSend: function (xhr, settings) {
                                spa.showLoading();
                            },
                            success: function (res) {
                                spa.hideLoading();
                                if (res.code === 1) {
                                    mui.alert("提交订单成功", '系统提示', function () {
                                        window.location.href = order_url;
                                    }, 'div');
                                } else {
                                    if (res.data === -1) {
                                        mui.confirm(res.msg, '巴马提示', ['取消', '前去充值'], function (e) {
                                            if (e.index === 1) {
                                                window.location.href = res.url;//todo 跳转到充值
                                            }
                                        });
                                    } else if (res.data === -2) {
                                        mui.alert(res.msg, '系统提示', function () {
                                            window.location.href = res.url;
                                        }, 'div');
                                    } else {
                                        mui.alert(res.msg);
                                    }
                                }
                            },
                            complete: function (XMLHttpRequest, textStatus) {
                                spa.hideLoading();
                            }
                        });
                    }
                });
                document.querySelectorAll('.mui-popup-input input')[0].type = 'password';
                document.querySelectorAll('.mui-popup-input input')[0].placeholder = '请输入支付密码';
            },
            wechat_item_pay: function (id) {
                spa.showLoading();
                var data = {
                    order_item_id: id
                };
                $.post(wechat_item_pay_url, data, function (res) {
                        spa.hideLoading();
                        if (res.code === 1) {
                            jsApiParameters = res.data;
                            if (typeof WeixinJSBridge == "undefined") {
                                if (document.addEventListener) {
                                    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                                } else if (document.attachEvent) {
                                    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                                    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                                }
                            } else {
                                jsApiCall();
                            }
                        } else {
                            mui.alert(res.msg, '系统提示');
                        }
                    }
                )
            }
        }
    });
});